/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package py.com.spadons.esmetil.security;

import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 *
 * @author spadons
 */
@ManagedBean(name = "securityBean")
@SessionScoped
public class SecurityBean implements Serializable{

    private boolean isUserAdmin = false;
    private boolean isUserGuest = false;

    public boolean getIsUserAdmin() {
        isUserAdmin = false;
        Authentication authentication = SecurityContextHolder.getContext()
                .getAuthentication();
        for (GrantedAuthority grantedAuthority : authentication
                .getAuthorities()) {
            if ("ROLE_SUPER_USUARIO".equals(grantedAuthority.getAuthority())) {
                isUserAdmin = true;
                break;
            }
        }
        return isUserAdmin;
    }

    public boolean getIsUserGuest() {
        isUserGuest = false;
        Authentication authentication = SecurityContextHolder.getContext()
                .getAuthentication();
        for (GrantedAuthority grantedAuthority : authentication
                .getAuthorities()) {
            if ("ROLE_GUEST".equals(grantedAuthority.getAuthority())) {
                isUserGuest = true;
                break;
            }
        }
        return isUserGuest;
    }

    public void setUserAdmin(boolean isUserAdmin) {
        this.isUserAdmin = isUserAdmin;
    }

    public void setUserGuest(boolean isUserGuest) {
        this.isUserGuest = isUserGuest;
    }
}
